// import com.alibaba.druid.sql.ast.SQLName;
// import com.alibaba.druid.sql.ast.statement.*;
// import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "HiveInsert.h"
#include "../../../ast/statement/SQLExprTableSource.h"
#include "../../../ast/statement/SQLSelect.h"
#include "../../../ast/statement/SQLAssignItem.h"
#include "../../../ast/statement/SQLInsertStatement.h"
#include "../../../../utils/instanceof.h"
#include "../../../../utils/listTran.h"
#include "../visitor/HiveASTVisitor.h"

HiveInsert::HiveInsert()
{
  partitions = std::make_shared<std::list<SQLAssignItem_ptr>>();
}

void HiveInsert::setPartitions(SQLAssignItem_list_ptr partitions)
{
  this->partitions = partitions;
}

// @Override
SQLObject_ptr HiveInsert::clone()
{
  // HiveInsert x = new HiveInsert();
  SQLObject_ptr clone_tmp = std::shared_ptr<HiveInsert>(new HiveInsert());
  std::shared_ptr<HiveInsert> x = std::dynamic_pointer_cast<HiveInsert>(clone_tmp);
  cloneTo(x);
  return x;
}

SQLExprTableSource_ptr HiveInsert::getTableSource()
{
  return tableSource;
}

void HiveInsert::setTableSource(SQLExprTableSource_ptr tableSource)
{
  if (tableSource != nullptr)
  {
    tableSource->setParent(SharedObject(HiveInsert));
  }
  this->tableSource = tableSource;
}

void HiveInsert::setTableSource(SQLName_ptr tableName)
{
  this->setTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(tableName)));
}

SQLSelect_ptr HiveInsert::getQuery()
{
  return query;
}

void HiveInsert::setQuery(SQLSelect_ptr query)
{
  if (query != nullptr)
  {
    query->setParent(SharedObject(HiveInsert));
  }
  this->query = query;
}

SQLInsertStatement_ValuesClause_list_ptr HiveInsert::getValuesList()
{
  return valuesList;
}

void HiveInsert::addValueCause(SQLInsertStatement_ValuesClause_ptr valueClause)
{
  if (valueClause != nullptr)
  {
    valueClause->setParent(SharedObject(HiveInsert));
  }
  valuesList->push_back(valueClause);
}

// @Override
void HiveInsert::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, HiveASTVisitor>(visitor))
  {
    accept0(std::dynamic_pointer_cast<HiveASTVisitor>(visitor));
  }
  else
  {
    acceptChild(visitor, tableSource);
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLAssignItem, SQLObject>(partitions);
    acceptChild(visitor, tmp1);
    SQLObject_list_ptr tmp2 = ListTran::tran_class<SQLInsertStatement_ValuesClause, SQLObject>(valuesList);
    acceptChild(visitor, tmp2);
    acceptChild(visitor, query);
  }
}

void HiveInsert::accept0(HiveASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(HiveInsert)))
  {
    acceptChild(visitor, tableSource);
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLAssignItem, SQLObject>(partitions);
    acceptChild(visitor, tmp1);
    SQLObject_list_ptr tmp2 = ListTran::tran_class<SQLInsertStatement_ValuesClause, SQLObject>(valuesList);
    acceptChild(visitor, tmp2);
    acceptChild(visitor, query);
  }
  visitor->endVisit(SharedObject(HiveInsert));
}
